﻿from simcell.cell.protein_bag import ProteinBag

class Membrane:
  """Membrana celular"""
  
  def __init__(self, cell):
    self.cell = cell
    self.freeProteins = ProteinBag()
    self.joinProteins = []
  
  def __str__(self):
    return "Membrana %d" % self.cell.id
  
  def doJoin(self, protein, membrane):
    self.joinProteins.append((protein, membrane))
    self.freeProteins.substract(protein)
  
  def interact(self, other_membrane):
    num_interacts = 0
    # optimization, loop over shorter dict ;-)
    if len(other_membrane.freeProteins.bag) < len(self.freeProteins.bag):
      other_membrane.interact(self)
    else:
      for protein, n in self.freeProteins.bag.iteritems():
        if n > 0 and other_membrane.freeProteins.getAmountOfProtein(protein) > 0:
          num_interacts += 1
          self.doJoin(protein, other_membrane)
          other_membrane.doJoin(protein, self)
          
    return num_interacts
